package csu.web.mypetstore.web.servlet;

import csu.web.mypetstore.domain.Account;
import csu.web.mypetstore.service.LogService;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class RegisterServlet extends HttpServlet {
    private String username;
    private String password;

    private String email;
    private String firstName;
    private String lastName;
    private String status;
    private String address1;
    private String address2;
    private String city;
    private String state;
    private String zip;
    private String country;
    private String phone;
    private String favouriteCategoryId;
    private String languagePreference;
    private String bannerName;
    private static final String REGISTER_FORM = "/WEB-INF/jsp/account/register.jsp";

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();

        //this.userid=req.getParameter("userid");
        this.username=req.getParameter("username");
        this.password=req.getParameter("password");
        this.email=req.getParameter("email");
        this.firstName=req.getParameter("firstName");
        this.lastName=req.getParameter("lastName");
        this.status=req.getParameter("status");
        this.address1=req.getParameter("address1");
        this.address2=req.getParameter("address2");
        this.city=req.getParameter("city");
        this.state=req.getParameter("state");
        this.zip=req.getParameter("zip");
        this.country=req.getParameter("country");
        this.phone=req.getParameter("phone");
        this.favouriteCategoryId=req.getParameter("favouriteCategoryId");
        this.languagePreference=req.getParameter("languagePreference");

        this.bannerName=req.getParameter("bannerName");

        List<String> languages = new ArrayList<String>();
        languages.add("english");
        languages.add("japanese");
        session.setAttribute("languages",languages);

        List<String> categories = new ArrayList<>();
        categories.add("FISH");
        categories.add("DOGS");
        categories.add("REPTILES");
        categories.add("CATS");
        categories.add("BIRDS");
        session.setAttribute("categories",categories);

        Account account = (Account) session.getAttribute("account");
        if(account != null) {
            String strUrl = "http://" + req.getServerName() + ":" + req.getServerPort()
                    + req.getContextPath() + req.getServletPath() + "?" + req.getQueryString();
            LogService logService = new LogService();
            String logInfo = logService.logInfo(" ") +"用户" +account.getUsername()+"  " + "注册页面";
            logService.insertLogInfo(account.getUsername(),logInfo);
        }
        req.getRequestDispatcher(REGISTER_FORM).forward(req,resp);
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}


